<?php
class Admin_MediaController extends Admin_Libraries_Controller_Action {
	
	protected $_config = null;
	
	public function init() {
		parent::init();
		$this->_config = Zend_Registry::get('configuration');		
		$this->view->config = $this->_config;
	}
	
	public function indexAction() {	
		$this->view->headLink ()->appendStylesheet ( $this->view->baseUrl ().'/html/admin/css/media.css' );
		$this->view->headLink ()->appendStylesheet ( $this->_config->site->jsUrl.'fancybox/fancybox.css' );
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/mousewheel.pack.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/fancybox.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jmedia.js', 'text/javascript');
		
		$cAlbum = new Admin_Model_Album();
		$albumWhere[] = "created_by={$this->_auth->id} 
						OR FILE_IN_SET('{$this->_auth->role_id}', role_access) > 0
						OR role_access='*'";		
		$this->view->arrAlbum = $cAlbum->getList();
		if (sizeof($this->view->arrAlbum)) {
			$this->view->currentAlbum = $this->view->arrAlbum[0];				
		}		
	}
	
	public function createAlbumAction() {
		$this->_helper->layout()->disableLayout();
		$this->view->headLink()->offsetUnset(0);
		$this->view->headLink ()->appendStylesheet ( $this->view->baseUrl ().'/html/admin/css/media.css' );
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jform.js', 'text/javascript');
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
		$cRole = new Admin_Model_Role();
		if ($this->getRequest()->isPost()) {		
			$post = $this->getRequest()->getPost();
			$notEmpty = new Zend_Validate_NotEmpty();
			$valid = TRUE;
			if (!$notEmpty->isValid($post['name'])) $valid = FALSE;
			if ($valid) {				
				$data = array();
				$data['name'] = $post['name'];
				if (empty($post['cid'])) $data['role_access'] = '*';
				else $data['role_access'] = implode(',', $post['cid']);				
				$cAlbum = new Admin_Model_Album();
				$response = $cAlbum->insert($data, $this->_auth->id);	
				if ($response > 0) {
					$flashMessage->addMessage('admin_save_successfull');
					$this->view->status = 'finish';	
				} else $flashMessage->addMessage('admin_save_fail');			
			}
		}		
		
		$this->view->arrRole = $cRole->getList();
	}
	
	public function reloadAlbumAction() {
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);		
		$cAlbum = new Admin_Model_Album();
		$albumWhere[] = "created_by={$this->_auth->id} 
						OR FILE_IN_SET('{$this->_auth->role_id}', role_access) > 0
						OR role_access='*'";		
		$arrAlbum = $cAlbum->getList();		
		$albumHtml = '';
		if (sizeof($arrAlbum)) {
			foreach ($arrAlbum as $item) {
				$albumHtml .= '<li class="folder">
									<a title="'.$item['name'].'" rel="/admin/media/load-album/id/'.$item['id'].'" 
									href="javascript:void(0);">'.$item['name'].'</a>
							  </li>';
			}
		} else $albumHtml = '<li class="folder">No album</li>';
		$response = array('album'=>$albumHtml);
		echo Zend_Json::encode($response);
		exit();
	}
	
	public function reloadImageAction() {
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$aid = $this->getRequest()->getParam('aid', 0);
		$cImage = new Admin_Model_Image();
		$cAlbum = new Admin_Model_Album();
		
		$album = $cAlbum->getDetail($aid);
		$html = '';
		if ($aid > 0) {
			$arrImage = $cImage->getListByAlbumId($aid);			
			if (sizeof($arrImage)) {
				$index = 0;
				foreach ($arrImage as $item) {
					$item['index'] = $index++;					
					$html .= $this->view->partial('media/image-item.phtml', array('image' => $item));
				}
			}			
		}		
		$response = array('album' => $album, 'image' => $html);
		echo Zend_Json::encode($response);
		exit();
	}
	
	public function uploadImageAction() {
		$this->_helper->layout()->disableLayout();		
		$this->view->headLink ()->appendStylesheet ( $this->view->baseUrl ().'/html/js/swfupload/css/default.css' );
		$this->view->headLink ()->appendStylesheet ( $this->view->baseUrl ().'/html/admin/css/media.css' );
		$this->view->headScript ()->appendFile ( $this->view->baseUrl () . '/html/js/jlib.js', 'text/javascript');
		$this->view->headScript ()->appendFile ( $this->view->baseUrl () . '/html/js/swfupload/swfupload.js', 'text/javascript');
		$this->view->headScript ()->appendFile ( $this->view->baseUrl () . '/html/js/swfupload/swfupload.queue.js', 'text/javascript');
		$this->view->headScript ()->appendFile ( $this->view->baseUrl () . '/html/js/swfupload/fileprogress.js', 'text/javascript');
		$this->view->headScript ()->appendFile ( $this->view->baseUrl () . '/html/js/swfupload/handlers.js', 'text/javascript');
		$this->view->albumId = $this->getRequest()->getParam('aid', 0);		
		$this->view->config = $this->_config;	
	}
	
	public function deleteImageAction() {	
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$id = $this->getRequest()->getParam('id', 0);
		if ($id <= 0) {
			echo 'false';
			exit();
		}		
		$cImage = new Admin_Model_Image();
		$detail = $cImage->getDetail($id);
		if (sizeof($detail)) {
			$config = Zend_Registry::get('configuration');
			if ($detail['file_name'] != '') {
				@unlink($config->site->upload->image->thumb_dir . $detail['file_name']);
				@unlink($config->site->upload->image->dir . $detail['file_name']);
			}		
			$response = $cImage->delete($id);
			if ($response > 0) echo 'true';
			else echo 'false';
		}
		exit();		
	}
	
	public function popupImageAction() {
		$this->_helper->layout()->disableLayout();
		$this->view->headLink ()->appendStylesheet ( $this->view->baseUrl ().'/html/admin/css/media.css' );
		$this->view->headLink ()->appendStylesheet ( $this->_config->site->jsUrl.'fancybox/fancybox.css' );
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/mousewheel.pack.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/fancybox.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jmedia.js', 'text/javascript');
		
		$cAlbum = new Admin_Model_Album();
		$albumWhere[] = "created_by={$this->_auth->id}
						OR FILE_IN_SET('{$this->_auth->role_id}', role_access) > 0
						OR role_access='*'";
		$this->view->arrAlbum = $cAlbum->getList();
		if (sizeof($this->view->arrAlbum)) {
			$this->view->currentAlbum = $this->view->arrAlbum[0];
		}
	}
	
	public function downloadImageAction() {
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$id = $this->getRequest()->getParam('id', 0);
		if ($id <= 0) {
			echo 'false';
			exit();
		}
		$cImage = new Admin_Model_Image();
		$detail = $cImage->getDetail($id);
		$config = Zend_Registry::get('configuration');
		$filePath = $config->site->upload->image->dir . $detail['file_name'];		
		if ($detail['file_name'] != '' && file_exists($filePath)) {			
			$contentType = '';
			switch (Core_Utilities_Image::getExtension($detail['name'])) {
				case 'jpg':
					$contentType = 'image/jpeg';
					break;
				case 'png':
					$contentType = 'image/png';
					break;
				case 'gif':
					$contentType = 'image/gif';
					break;
				default:
					$contentType = 'image/jpeg';					
			}
			$this->getResponse()
				 ->setHeader('Content-Disposition', 'attachment; filename="'.$detail['name'].'"')
				 ->setHeader('Content-Transfer-Encoding', 'binary')
				 ->setHeader('Content-Length', filesize($filePath))
				 ->setHeader('Content-type', $contentType);					
			$this->getResponse()->sendHeaders();			
			readfile($filePath);
			exit;
		}
	}
}